﻿using SGArchiveSystem7x.App_Code;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.Word
{
    public class ArchiveWordController : BaseController
    {

        /// <summary>
        /// 本控制器的名称，用于权限访问
        /// </summary>
        private string sControllerName = "ArchiveWord";
        // GET: ArchiveWord
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            SGArchiveSystem7x.App_Code.CSession.Clear_PageSession(Session);
            
            //词典类别
            var type = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxWordTypeTree(false);
            ViewBag.Type = type;
            return View();
        }
        [HttpPost]
        public JsonResult Get_ajaxCom()
         {
            var list = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxWordTypeTree(false);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 显示词典内容
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxDictionaryContent(int type)
        {
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_ArchiveWord(out count, type, 0, CWebCommons._iPageRows);
            var listWhere = new List<string>();
            listWhere.Add(type.ToString());
            App_Code.CSession.Add_QueryWhere(Session, listWhere);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="num"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxDictionaryContentPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_ArchiveWordDal.Get_ArchiveWord(out count, int.Parse(listWhere[0]), iStart, CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Get_ajaxArchiveWordType(string TypeName)
        {
            T_ArchiveWordType wt = new T_ArchiveWordType()
            {
                WT_NAME = TypeName,
                WT_SYSTEM = 1,
                WT_STATUS = 1
            };
            var value = (int)T_ArchiveWordTypeDal.Add_ArchiveWordType(wt);
            return value.ToString();
        }

        /// <summary>
        /// 添加内容
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Get_ajaxAddWord(string Name, int Order, int Type, string Value)
        {
            int? iValue = null;
            if (Value == "")
                iValue = null;
            else
            {
                try
                {
                    iValue = int.Parse(Value);
                }
                catch { iValue = null; }
            }
            T_ArchiveWord w = new T_ArchiveWord()
            {
                WD_NAME = Name,
                WD_ORDER = Order,
                WD_TYPE = Type,
                WD_VALUE = iValue
            };
            var value = (int)T_ArchiveWordDal.Add_ArchiveWord(w);
            return value.ToString();
        }

        public string Get_ajaxModifyWord(int id, string Name, int Order, string Value, int type)
        {
            int? iValue = null;
            if (Value == "")
                iValue = null;
            else
            {
                try
                {
                    iValue = int.Parse(Value);
                }
                catch { iValue = null; }
            }
            T_ArchiveWord w = new T_ArchiveWord()
            {
                WD_ID = id,
                WD_NAME = Name,
                WD_ORDER = Order,
                WD_VALUE = iValue,
                WD_TYPE = type
            };
            int value = (int)T_ArchiveWordDal.Modify_ArchiveWord(w);
            return  value.ToString();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Del_ajaxArchiveWord(int id)
        {
            var value = (int)T_ArchiveWordDal.Del_ArchiveWord(id);
            return  value.ToString();
        }

        /// <summary>
        /// 获取当个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxArchiveWordId(int id)
        {
            var en = T_ArchiveWordDal.Get_ArchiveWord(id);
            return Json(en, JsonRequestBehavior.AllowGet);
        }
    }
}